using System;
using System.Web.Mvc;
using TimeManager.Domain.Entities;
using TimeManager.Web.Models;
using TimeManager.Web.ViewModels;


namespace TimeManager.Web.Controllers {

    public class TasksController : Controller {
        private readonly IUserRepository userRepository;
        private readonly IProjectRepository projectRepository;
        private readonly ITaskRepository taskRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public TasksController()
            : this(new UserRepository(), new ProjectRepository(), new TaskRepository()) {
        }

        public TasksController(IUserRepository userRepository, IProjectRepository projectRepository, ITaskRepository taskRepository) {
            this.userRepository = userRepository;
            this.projectRepository = projectRepository;
            this.taskRepository = taskRepository;
        }

        public ViewResult Index() {
            return View(taskRepository.AllIncluding(task => task.User, task => task.Project));
        }

        public ViewResult Details(int id) {
            return View(taskRepository.Find(id));
        }

        public ActionResult Create() {
            Task task = new Task();
            ViewBag.PossibleUsers = userRepository.All;
            ViewBag.PossibleProjects = projectRepository.All;
            return View(task);
        }

        [HttpPost]
        public ActionResult Create(Task task) {
            if (ModelState.IsValid) {
                taskRepository.InsertOrUpdate(task);
                taskRepository.Save();
                return RedirectToAction("Index");
            } else {
                ViewBag.PossibleUsers = userRepository.All;
                ViewBag.PossibleProjects = projectRepository.All;
                return View();
            }
        }

        public ActionResult Edit(int id) {
            ViewBag.PossibleUsers = userRepository.All;
            ViewBag.PossibleProjects = projectRepository.All;
            return View(taskRepository.Find(id));
        }

        [HttpPost]
        public ActionResult Edit(TaskViewModel t) {
            if (ModelState.IsValid) {
                var task = new Task();
                task.TaskId = t.TaskId;
                task.UserId = t.UserId;
                task.ProjectId = t.ProjectId;
                task.TimeStart = DateTime.Parse(t.TimeStartDate + " " + t.TimeStartTime);

                taskRepository.InsertOrUpdate(task);
                taskRepository.Save();
                return RedirectToAction("Index");
            } else {
                ViewBag.PossibleUsers = userRepository.All;
                ViewBag.PossibleProjects = projectRepository.All;
                return View();
            }
        }

        public ActionResult Delete(int id) {
            return View(taskRepository.Find(id));
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id) {
            taskRepository.Delete(id);
            taskRepository.Save();

            return RedirectToAction("Index");
        }

    }
}

